package com.tuku.picturesearch;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.example.tukuduang.R;


public class ShowPictureActivity extends Activity {
    private static final String TAG = "ShowPictureActivity";
    private ImageView image;

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreated()");
        setContentView(R.layout.show_picture_activity);
        image = (ImageView)findViewById(R.id.large_view);

        image.setImageBitmap(PicSearch.map_buffer);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 0, 0, "分享");
        return true;
    }

  
    private Uri saveImage() {
        if(image.getDrawable() != null) {
            File jpg_file;
            BufferedOutputStream output_stream;
            Bitmap bitmap;
            try {
                jpg_file = new File(Environment.getExternalStorageDirectory(), "buffer.jpg");
                output_stream = new BufferedOutputStream(new FileOutputStream(jpg_file));
                bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 80, output_stream);
                output_stream.flush();
                output_stream.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
            return Uri.fromFile(jpg_file);
        }
        return null;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 0:
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("image/jpg");
                intent.putExtra(Intent.EXTRA_SUBJECT, "Share");
                Uri uri = saveImage();
                if(uri != null) {
                    intent.putExtra(Intent.EXTRA_STREAM, uri);
                    startActivity(Intent.createChooser(intent, getTitle()));
                    return true;
                }
        }
        return false;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
